package com.cr.crandroid.adapter;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cr.crandroid.R;
import com.cr.crandroid.utils.CommonConstant;
import com.cr.crandroid.vo.RentCarVo;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AppointmentAdapter extends BaseQuickAdapter<RentCarVo, BaseViewHolder> {

    private MaterialAlertDialogBuilder materialAlertDialog;
    private RentCarVo rentCarVo;

    public AppointmentAdapter(@Nullable List<RentCarVo> data) {
        super(R.layout.item_appointment, data);
    }

    public AppointmentAdapter(@Nullable List<RentCarVo> data, MaterialAlertDialogBuilder materialAlertDialog) {
        super(R.layout.item_appointment, data);
        this.materialAlertDialog = materialAlertDialog;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, RentCarVo rentCarVo) {
        baseViewHolder.setText(R.id.textView, rentCarVo.getRealName());
        Button button = baseViewHolder.getView(R.id.button);
        button.setVisibility(View.GONE);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date nowDate = new Date();
            Date startDate = sdf.parse(rentCarVo.getStartTime());
            Date endDate = sdf.parse(rentCarVo.getEndTime());
            if (startDate.getTime() < nowDate.getTime() && endDate.getTime() > nowDate.getTime()) {
                baseViewHolder.setText(R.id.textView2, "交易中");
            } else if (endDate.getTime() < nowDate.getTime()) {
                baseViewHolder.setText(R.id.textView2, "确认中");
            } else {
                button.setVisibility(View.VISIBLE);
                baseViewHolder.setText(R.id.textView2, "等待中");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        switch (rentCarVo.getStatus()) {
            case CommonConstant.RENT_2:
                button.setVisibility(View.GONE);
                baseViewHolder.setText(R.id.textView2, "交易结束");
                break;
            case CommonConstant.RENT_3:
                button.setVisibility(View.GONE);
                baseViewHolder.setText(R.id.textView2, "取消交易");
                break;
        }
        baseViewHolder.setText(R.id.textView3, rentCarVo.getBrand());
        baseViewHolder.setText(R.id.textView4, rentCarVo.getColor());
        baseViewHolder.setText(R.id.textView5, rentCarVo.getSeatingCapacity());
        baseViewHolder.setText(R.id.textView6, rentCarVo.getPrice());
        baseViewHolder.setText(R.id.textView7, rentCarVo.getStartTime() + "至" + rentCarVo.getEndTime());
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showMaterialAlertDialog(rentCarVo);
            }
        });
        if (rentCarVo.getImage() != null)
            Glide.with(getContext()).load(rentCarVo.getImage().split(",")[0])
                    .into((ImageView) baseViewHolder.getView(R.id.imageView));
    }

    private void showMaterialAlertDialog(RentCarVo rentCarVo) {
        this.rentCarVo = rentCarVo;
        materialAlertDialog.show();
    }

    public RentCarVo getRentCarVo() {
        return rentCarVo;
    }
}
